using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public class tbdonhangDAL
    {
        private SqlConnection cn = null;
        public tbdonhangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbdonhangDAL(tbdiemdenDAL thach)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbdonhang donhang)
        {
            SqlCommand cm = new SqlCommand("donhang_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@masanpham", donhang.Masanpham);
            cm.Parameters.AddWithValue("@manguoidung", donhang.Manguoidung);
            cm.Parameters.AddWithValue("@mahinhthucthanhtoan", donhang.Mahinhthucthanhtoan);
            cm.Parameters.AddWithValue("@matinhtrang", donhang.Matinhtrang);
            cm.Parameters.AddWithValue("@diachigiaohang", donhang.Diachigiaohang);
            cm.Parameters.AddWithValue("@ngaygiodathang", donhang.Ngaygiodathang);
            cm.Parameters.AddWithValue("@ngaygiohuy", donhang.Ngaygiohuy);
            cm.Parameters.AddWithValue("@hotenkhachhang", donhang.Hotenkhachhang);
            cm.Parameters.AddWithValue("@diachikhachhang", donhang.Diachikhachhang);
            cm.Parameters.AddWithValue("@emailkhachhang", donhang.Emailkhachhang);
            cm.Parameters.AddWithValue("@dienthoaikhachhang", donhang.Dienthoaikhachhang);
            cm.Parameters.AddWithValue("@soluong", donhang.Soluong);
            cm.Parameters.AddWithValue("@tongtien", donhang.Tongtien);
            cm.ExecuteNonQuery();
        }
        public void Update(tbdonhang donhang)
        {
            SqlCommand cm = new SqlCommand("donhang_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madonhang", donhang.Madonhang);
            cm.Parameters.AddWithValue("@manguoidung", donhang.Manguoidung);
            cm.Parameters.AddWithValue("@mahinhthucthanhtoan", donhang.Mahinhthucthanhtoan);
            cm.Parameters.AddWithValue("@matinhtrang", donhang.Matinhtrang);
            cm.Parameters.AddWithValue("@diachigiaohang", donhang.Diachigiaohang);
            cm.Parameters.AddWithValue("@ngaygiodathang", donhang.Ngaygiodathang);
            cm.Parameters.AddWithValue("@ngaygiohuy", donhang.Ngaygiohuy);
            cm.Parameters.AddWithValue("@hotenkhachhang", donhang.Hotenkhachhang);
            cm.Parameters.AddWithValue("@diachikhachhang", donhang.Diachikhachhang);
            cm.Parameters.AddWithValue("@emailkhachhang", donhang.Emailkhachhang);
            cm.Parameters.AddWithValue("@dienthoaikhachhang", donhang.Dienthoaikhachhang);
            cm.Parameters.AddWithValue("@soluong", donhang.Soluong);
            cm.Parameters.AddWithValue("@tongtien", donhang.Tongtien);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("donhang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madonhang", iD);
            cm.ExecuteNonQuery();
        }
        public tbdonhangcollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("donhang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbdonhangcollection diemden1Collection = new tbdonhangcollection();
            while (dr.Read())
            {
                tbdonhang donhang = new tbdonhang();
                donhang.Madonhang= int.Parse(dr.GetValue(0).ToString());
                donhang.Masanpham = int.Parse(dr.GetValue(1).ToString());
                donhang.Manguoidung = int.Parse(dr.GetValue(2).ToString());
                donhang.Mahinhthucthanhtoan = int.Parse(dr.GetValue(3).ToString());
                donhang.Matinhtrang = int.Parse(dr.GetValue(4).ToString());
                donhang.Diachigiaohang = dr.GetValue(5).ToString();
                donhang.Ngaygiodathang = DateTime.Parse(dr.GetValue(6).ToString());
                donhang.Ngaygiohuy = DateTime.Parse(dr.GetValue(7).ToString());
                donhang.Hotenkhachhang = dr.GetValue(8).ToString();
                donhang.Diachikhachhang = dr.GetValue(9).ToString();
                donhang.Emailkhachhang = dr.GetValue(10).ToString();
                donhang.Dienthoaikhachhang = dr.GetValue(11).ToString();
                donhang.Soluong = int.Parse(dr.GetValue(12).ToString());
                donhang.Tongtien = float.Parse(dr.GetValue(13).ToString());
                diemden1Collection.Add(donhang);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return diemden1Collection;
        }
    }
}
